/*
 * =====================================================================================
 *
 *       Filename:  test.c
 *
 *    Description:  read testbench
 *        Version:  1.0
 *
 *        Created:  04/04/2013 08:09:07 PM
 *       Compiler:  gcc
 *
 *         Author:  Daohang Shi
 *
 * =====================================================================================
 */

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>

 /**
  * A structure to represent a 3D Point. 
  */
 typedef struct
 {
   int x ; /* x coordinate ( >=0 in the routing grid)*/
   int y ; /* y coordinate ( >=0 in the routing grid)*/

 } point ;

point   makept ( int x, int y )
{
        point   temp;
        temp.x = x;
        temp.y = y;
        return  temp;
}

  /**
  * A structure to represent a segment
  */
 typedef struct
 {
   point p1 ; 	/* start point of a segment */
   point p2 ; 	/* end point of a segment */
   
   int numEdges ; 	/* number of edges in the segment*/
   int *edges ;  	/* array of edges representing the segment*/
   
 } segment ;
 
 
  /**
  * A structure to represent a route
  */
  typedef struct
  {
    int numSegs ;  	/* number of segments in a route*/
    segment *segments ;  /* an array of segments (note, a segment may be flat, L-shaped or any other shape, based on your preference */

  } route ;
 
 
  /**
  * A structure to represent nets
  */
  typedef struct
  {
   int id ; 		/* ID of the net */
   int numPins ; 	/* number of pins (or terminals) of the net */
   point *pins ; 	/* array of pins (or terminals) of the net. */
   int numCRoutes ; 	/* number of (candidate) routes of the net. This may be equal to one (only one candidate route) in your implementation. */
   route *croutes ;	/* array of candidate routes of the net. */

  } net ;
  
  /**
  * A structure to represent the routing instance
  */
  typedef struct
  {
   int gx ;		/* x dimension of the global routing grid */
   int gy ;		/* y dimension of the global routing grid */
   
   int cap ;
   
   int numNets ;	/* number of nets */
   net *nets ;		/* array of nets */
   
   int numEdges ; 	/* number of edges of the grid */
   int *edgeCaps; 	/* array of the actual edge capacities after considering for blockages */
   int *edgeUtils;	/* array of edge utilizations */  
  } routingInst ;
#define LEN     60
#define PTR     printf ( "%s\n", str )

int main()
{
        FILE            *fp = fopen("adaptec1_s13.gr","r");
        char            line[LEN];
        int             gx, gy;
        int             cap;
        int             num_net;
        int             cnt_net;
        int             cnt_pin;
        int             index;
        net             *net;


        const   char    grid_line[]  = "grid";
        const   char    cap_line[]   = "capacity";
        const   char    num_line[]   = "num net";

        while ( fgets ( line, LEN, fp ) )
        {

                char    *first_space = strchr ( line,' ' );
                char    *last_space = strrchr ( line,' ' );
                char    str[LEN];


                if ( !strncmp (line, grid_line, strlen(grid_line)) )
                {
                        memcpy ( str, first_space+1, last_space-first_space-1 );
                        gx = atoi (str); 
                        memcpy ( str, last_space+1, LEN );
                        gy = atoi (str); 
/*                                                                         
                        grid = calloc ( gx, sizeof(point*) );
                        int     i;
                        for (i = 0; i < gx; ++i)
                                grid[i] = calloc ( gy, sizeof(point) );
//                        printf("%d %d\n", gx, gy);
 */
                        continue;
                }
                if ( !strncmp (line, cap_line, strlen(cap_line)) )      /* read default capacity */
                {
                        memcpy ( str, last_space+1, LEN );
                        cap = atoi(str);
                        continue;
                }
                if ( !strncmp ( line, num_line, strlen(num_line) ) )    /* read num net */
                {
                        memcpy ( str, last_space+1, LEN );
                        num_net = atoi  (str);
                        net = calloc( num_net, sizeof(net) );
                        cnt_net = 0;
                        continue;
                }
                if ( !strncmp ( line, "n", 1 ) )                        /* read net info */
                {
                        ++cnt_net;
                        memcpy ( str, line+1, first_space-line );
                        index = atoi (str);        
                        net[index].id = index;  /* net.index */
                        memcpy ( str, first_space+1, LEN );
                        net[index].numPins = atoi (str); /* net.numPins */
                        net[index].pins = calloc ( atoi(str), sizeof(point) ); /* net.pins */
//                        printf ( "id = %d, numPins = %d\n", net[index].id, net[index].numPins );
                        cnt_pin = 0;
                        continue;
                }
                if ( cnt_net <= num_net )                               /* read segment */
                {
                        memcpy ( str, line, first_space-line+1 );
                        int x = atoi(str);
                        memcpy ( str, first_space+1, LEN);
                        int y = atoi(str);
                        
                        net[index].pins[cnt_pin] = makept(x, y);
                        printf ( "net[%d].pins[%d] : %d %d\n", index, cnt_pin, x, net[index].pins[cnt_pin].y);
                        ++ cnt_pin;
                }
        }
        printf ( "helloworld\n" );
//        printf ( "finally, cnt_net = %d\n", cnt_net );
        return 1;
}
